﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraNavBar;
using Model;

namespace AccManagerKw
{
    public partial class Test : Form
    {
        public Test()
        {
            InitializeComponent();
        }

        private void Test_Shown(object sender, EventArgs e)
        {
            try
            {
                rcMenu.Pages.Clear();
                FillMenu();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        private void FillMenu()
        {
            using (var context = new AccManagerEntities())
            {
                var lst = context.MenuMasters.ToList();
                foreach (var menu in lst.Where(x => x.ParentId == 0).OrderBy(x => x.MenuId))
                {
                    rcMenu.Pages.Add(new RibbonPage(menu.MenuName) { Tag = menu.MenuId });
                }

                foreach (RibbonPage page in rcMenu.Pages)
                {
                    if (page != null)
                    {
                        var subMenu = lst.Where(x => x.ParentId == Convert.ToInt64(page.Tag));
                        foreach (var menu in subMenu)
                        {
                            var index = page.Groups.Add(new RibbonPageGroup(menu.MenuName) { Tag = menu.MenuId });
                            foreach (var innerMenu in lst.Where(x => x.ParentId == menu.MenuId))
                            {
                                var bItem = page.Groups[index].ItemLinks.Add(new BarButtonItem()
                                {
                                    Caption = innerMenu.MenuName,
                                    Tag = innerMenu.MenuId
                                });
                                bItem.Item.ItemClick += OwnerItem_ItemClick;
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Group Is Null");
                    }
                }
                //foreach (var VARIABLE in COLLECTION)
                //{

                //}
            }
        }

        void OwnerItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                Console.WriteLine("MenuId -> {0}", e.Item.Tag.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
